﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace ExplorerPlus.Configuration
{
    public static class ConfigurationSettings
    {
        private static Settings settings;

        /// <summary>
        /// Retrieves user preferences from settings.xml file.
        /// </summary>
        public static Settings AppSettings
        {
            get
            {
                if (settings == null)
                {
                    lock (new object())
                    {
                        if (settings == null)
                        {
                            if (File.Exists("settings.xml"))
                            {
                                try
                                {
                                    StreamReader stream = new StreamReader("settings.xml");
                                    XmlSerializer xSerializer = new XmlSerializer(typeof(Settings));
                                    settings = (Settings)xSerializer.Deserialize(stream);
                                    stream.Close();
                                }
                                catch
                                {
                                    InitializeSettings();
                                }
                            }
                            else
                            {
                                InitializeSettings();
                            }
                        }
                    }
                }
                return settings;
            }
        }

        /// <summary>
        /// Stores changes made to user preferences to settings.xml file.
        /// </summary>
        public static void SaveChanges()
        {
            try
            {
                StreamWriter stream = new StreamWriter("settings.xml");
                XmlSerializer xSerializer = new XmlSerializer(typeof(Settings));
                xSerializer.Serialize(stream, settings);
                stream.Close();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex, "Error");
            }
        }

        /// <summary>
        /// Set default value to user preferences.
        /// </summary>
        private static void InitializeSettings()
        {
            settings = new Settings();
            settings.AlwaysShowTrayIcon = true;
            settings.BooksFilter = "*.*";
            settings.CloseToTray = false;
            settings.ConfirmForChangesMade = true;
            settings.ConfirmWhenDeletingItems = true;
            settings.DefaultCategory = "Books";
            settings.GamesFilter = "*.*";
            settings.MinimizeToTray = true;
            settings.MoviesFilter = "*.*";
            settings.MusicFilter = "*.*";
            settings.SingleClickOnTrayToOpen = false;
            settings.AmazonLocales = "United States";
            settings.AdvancedSearch = true;
            settings.AutoSave = false;
        }

        public static string MoviesImagePath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Movies\\";
            }
        }
        public static string GamesImagePath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Games\\";
            }
        }
        public static string BooksImagePath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Books\\";
            }
        }
        public static string MusicImagePath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Music\\";
            }
        }
    }
}
